package com.nondo.dean.community.controller.api.admin;

import com.nondo.dean.common.annotation.ApiVersion;
import com.nondo.dean.common.tool.ValidateUtil;
import com.nondo.dean.common.web.Page;
import com.nondo.dean.common.web.Query;
import com.nondo.dean.common.web.View;
import com.nondo.dean.community.domain.Category;
import com.nondo.dean.community.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ken.
 * @version 1.0
 * @email 695093513@qq.com
 * @date 2018/3/28
 */
@RestController("apiAdminCommunityCategory")
@RequestMapping("/{v}/admin/category")
public class CategoryController {
    @Autowired
    CategoryService categoryService;

    /**
     * 列表
     *
     * @param request
     * @param response
     * @param query
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/list")
    public View<Page<Category>> list(HttpServletRequest request, HttpServletResponse response, @RequestBody Query<Category> query) {
        View<Page<Category>> view = new View<Page<Category>>(20000, "获取数据成功");

        Category category = query.getData();

        Page<Category> page = new Page<Category>(query.getPage(), query.getLimit(), categoryService.count(category));

        List<Category> gradeList = categoryService.getList(query.getOffset(), query.getLimit(), category);

        page.setContent(gradeList);

        view.setData(page);

        return view;
    }

    /**
     * 添加
     *
     * @param request
     * @param response
     * @param category
     * @param result
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/add")
    public View<Category> add(HttpServletRequest request, HttpServletResponse response, @Validated @RequestBody Category category, BindingResult result) {
        View view = new View(20000, "添加成功");

        if (result.hasErrors()) {
            view.setCode(40000);
            view.setMessage(ValidateUtil.first(result.getAllErrors()));
        } else {
            this.categoryService.add(category);
        }

        return view;
    }

    /**
     * 更新
     *
     * @param request
     * @param response
     * @param category
     * @param result
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/update")
    public View<Category> update(HttpServletRequest request, HttpServletResponse response, @Validated @RequestBody Category category, BindingResult result) {
        View view = new View(20000, "更新成功");

        if (result.hasErrors()) {
            view.setCode(40000);
            view.setMessage(ValidateUtil.first(result.getAllErrors()));
        } else {
            this.categoryService.update(category);
        }

        return view;
    }

    /**
     * 删除
     *
     * @param request
     * @param response
     * @param categoryId
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/delete/{categoryId}")
    public View<Category> delete(HttpServletRequest request, HttpServletResponse response, @PathVariable int categoryId) {
        View view = new View(20000, "删除成功");

        Category category = this.categoryService.select(categoryId);
        this.categoryService.delete(category);

        return view;
    }
}
